# -*- coding:utf-8 -*- 
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from view import *
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from SimpleXMLRPCServer import SimpleXMLRPCServer
import base64,md5
from mywork import *
from model import *
def saveimg(name,blob,summary,title,*args):
    cont=base64.b64decode(blob)
    n_md5=md5.md5(cont).hexdigest()
    if name:
        name=base64.b64decode(name)
        pass
    else:
        name=n_md5
    name=n_md5
    summary=base64.b64decode(summary)
    title=base64.b64decode(title)
    name=code_data(name)
    descrip=code_data(summary)
    title=code_data(title)
    tag=code_data(args[0])
    catalog=code_data(args[1])
    if blob:
        img=ablum()
        img.name=name
        img.blob=db.Blob(cont)
        img.descrip=db.Text(descrip)
        img.title=title
        img.tag=tag
        img.catalog=catalog
        img.put()
        #n_url=n_md5
        ret=u'''http://a-ablum.appspot.com/img/'''+name
        return ret 
    else:
        return 'http://s-album.appspot.com/img/88f5db500777fad5cc4770da32c0657f'
def testapi():
    c="yes api is readly"
    return  c


##------------------------------------------------------------------------------
class PlogXMLRPCDispatcher(SimpleXMLRPCDispatcher):
    def __init__(self, funcs):
        SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8')
        self.funcs = funcs

dispatcher = PlogXMLRPCDispatcher({'saveimg':saveimg,
                                   'testapi':testapi,})
class callapi(webapp.RequestHandler):
    def get(self):
        #Logger(request = self.request.uri, response = '----------------------------------').put()
        self.response.out.write('<h1>please use POST</h1>')
    def post(self):
        request = self.request.body
        response = dispatcher._marshaled_dispatch(request)
        self.response.out.write(response)


class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')


application = webapp.WSGIApplication([('/img/.*',quickimg),
				      ('/lazeimg/.*',img),
                                      ('/up',upimg),
                                      ('/rpc',callapi),
				      
                                      ('/', MainPage),
                                      ], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
